// pages/User/Alter/Alter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: '',
    name: '',
    gender: '',
    department: '',
    major: '', 
    number: '',
    level: '',
    nation: '',
    place: '',
    politics: '',
    identity: '',
    phone: ''
  },

  onLoad: function() {
    // 从 storage 中读取用户信息
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        name: userInfo.name,
        gender: userInfo.gender,
        department: userInfo.department,
        major: userInfo.major, 
        number: userInfo.number,
        level: userInfo.level,
        nation: userInfo.nation,
        place: userInfo.place,
        politics: userInfo.politics,
        identity: userInfo.identity,
        phone: userInfo.phone
      });
    }
  },

  // 处理性别选择
  radioChange: function(e) {
    this.setData({
      gender: e.detail.value === 'option1' ? '女' : '男'
    });
  },

  // 处理学习层次选择
  levelChange: function(e) {
    this.setData({
      level: e.detail.value === 'option1' ? '本科' : '专科'
    });
  },

  // 处理系别输入
  onDepartmentInput: function(e) {
    this.setData({
      department: e.detail.value
    });
  },

  // 处理班级输入
  onClassInput: function(e) {
    this.setData({
      major: e.detail.value
    });
  },

  // 处理民族输入
  onNationInput: function(e) {
    this.setData({
      nation: e.detail.value
    });
  },

  // 处理籍贯输入
  onPlaceInput: function(e) {
    this.setData({
      place: e.detail.value
    });
  },

  // 处理政治面貌输入
  onPoliticsInput: function(e) {
    this.setData({
      politics: e.detail.value
    });
  },

  // 处理身份证号输入
  onIdentityInput: function(e) {
    this.setData({
      identity: e.detail.value
    });
  },

  // 处理联系电话输入
  onPhoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    });
  },

  // 取消按钮
  Cancel: function() {
    wx.navigateBack();
  },

  // 保存按钮
  save: function() {
    const { nickname, name, gender, department, major, number, level, nation, place, politics, identity, phone } = this.data;
  
    // 发送请求更新用户信息
    wx.request({
      url: 'http://localhost:3000/updateUser', // 后端接口地址
      method: 'POST',
      data: {
        nickname,
        name,
        gender,
        department,
        major,
        number,
        level,
        nation,
        place,
        politics,
        identity,
        phone
      },
      success: (res) => {
        if (res.data.success) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          });
          // 更新本地存储的用户信息
          wx.setStorageSync('userInfo', {
            nickname,
            name,
            gender,
            department,
            major,
            number,
            level,
            nation,
            place,
            politics,
            identity,
            phone
          });
          wx.navigateBack();
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  }
});